﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XGame.Display
{
    public class XCamera
    {

        /// <summary>
        /// Initializes the camera class with the default settings
        /// </summary>
        public XCamera()
        {
            Zoom = 1;
            Position = Vector2.Zero;
            Rotation = 0;
            Origin = Vector2.Zero;
            Position = Vector2.Zero;
        }

        /// <summary>
        /// The zoom effect of the camera
        /// </summary>
        public Single Zoom { get; set; }

        /// <summary>
        /// The position of the camera
        /// </summary>
        public Vector2 Position { get; set; }

        /// <summary>
        /// The rotation of the camera, in radians
        /// </summary>
        public Single Rotation { get; set; }

        /// <summary>
        /// The origin of the camera
        /// </summary>
        public Vector2 Origin { get; set; }


        /// <summary>
        /// Adjusts the camera and instantly offsets it by the given Vector.
        /// Please note this is an additive call and will take into account
        /// whatever has been done prior.
        /// </summary>
        /// <param name="aDirection">The direction to offset the camera by</param>
        public void Move(Vector2 aDirection)
        {
            Position += aDirection;
        }

        /// <summary>
        /// Resets the camera back to its default position
        /// </summary>
        public void Reset()
        {
            Zoom = 1.0f;
            Position = Vector2.Zero;
            Rotation = 0;
        }

        /// <summary>
        /// Returns the Matrix transformation necessary for the SpriteBatch object
        /// </summary>
        /// <returns>A matrix that has applied the transformations necessary to adjust the camera</returns>
        public Matrix GetTransform()
        {
            var oTranslationMatrix = Matrix.CreateTranslation(new Vector3(Position.X, Position.Y, 0));
            var oRotationMatrix = Matrix.CreateRotationZ(Rotation);
            var oScaleMatrix = Matrix.CreateScale(new Vector3(Zoom, Zoom, 1));
            var oOriginMatrix = Matrix.CreateTranslation(new Vector3(Origin.X, Origin.Y, 0));
            return oTranslationMatrix * oRotationMatrix * oScaleMatrix * oOriginMatrix;
        }

    }
}
